package com.campus.event.controller;

import com.campus.event.entity.Event;
import com.campus.event.service.EventService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import java.io.OutputStream;
import jakarta.servlet.http.HttpServletResponse;
import com.campus.event.repository.RegistrationRepository;
import java.util.Map;
import java.util.HashMap;

@RestController
@RequestMapping("/api/events")
@RequiredArgsConstructor
public class EventController {
    private final EventService eventService;
    private final RegistrationRepository registrationRepository;

    @PostMapping
    @PreAuthorize("hasRole('ORGANIZER')")
    public ResponseEntity<Event> createEvent(@RequestBody Event event) {
        return ResponseEntity.ok(eventService.createEvent(event));
    }

    @PutMapping("/{id}")
    @PreAuthorize("hasRole('ORGANIZER')")
    public ResponseEntity<Event> updateEvent(@PathVariable Long id, @RequestBody Event event) {
        return ResponseEntity.ok(eventService.updateEvent(id, event));
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ORGANIZER')")
    public ResponseEntity<Void> deleteEvent(@PathVariable Long id) {
        eventService.deleteEvent(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/{id}")
    public ResponseEntity<Event> getEvent(@PathVariable Long id) {
        return ResponseEntity.ok(eventService.getEvent(id));
    }

    @GetMapping
    public ResponseEntity<List<Event>> getAllEvents() {
        return ResponseEntity.ok(eventService.getAllEvents());
    }

    @GetMapping("/type/{type}")
    public ResponseEntity<List<Event>> getEventsByType(@PathVariable String type) {
        return ResponseEntity.ok(eventService.getEventsByType(type));
    }

    @GetMapping("/{id}/qr-code")
    public void generateQRCode(@PathVariable Long id, HttpServletResponse response) throws Exception {
        String content = "eventId=" + id;
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        var bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, 300, 300);
        response.setContentType("image/png");
        OutputStream os = response.getOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, "PNG", os);
        os.flush();
        os.close();
    }

    @GetMapping("/{id}/stats")
    public Map<String, Object> getEventStats(@PathVariable Long id) {
        int registrationCount = registrationRepository.findByEventId(id).size();
        int maxParticipants = eventService.getEvent(id).getMaxParticipants();
        Map<String, Object> map = new HashMap<>();
        map.put("registrationCount", registrationCount);
        map.put("maxParticipants", maxParticipants);
        return map;
    }
} 